﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Rcompany.Model;
using Rcompany.SalesInvoiceDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Rcompany.SalesInvoices
{
    public class SalesInvoiceStore : ApplicationService, ISalesInvoices
    {
        public readonly IRepository<SalesInvoice, Guid> sales;
        public SalesInvoiceStore(IRepository<SalesInvoice, Guid> _sales)
        {
            sales = _sales;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<SalesInvoiceDto> Add(CreateUpdateSalesInvoiceDto dto)
        {
            var create = await sales.InsertAsync(ObjectMapper.Map<CreateUpdateSalesInvoiceDto, SalesInvoice>(dto));
            return ObjectMapper.Map<SalesInvoice, SalesInvoiceDto>(create);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task Del(Guid id)
        {
            await sales.DeleteAsync(id);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SalesInvoiceDto>> Show(string name)
        {
            if (name==null)
            {
                List<SalesInvoice> sale = await sales.ToListAsync();
                List<SalesInvoiceDto> dtos = ObjectMapper.Map<List<SalesInvoice>, List<SalesInvoiceDto>>(sale);
                return dtos;
            }
            else
            {
                List<SalesInvoice> sale = await sales.Where(x=>x.SalesOffice.Equals(name)).ToListAsync();
                List<SalesInvoiceDto> dtos = ObjectMapper.Map<List<SalesInvoice>, List<SalesInvoiceDto>>(sale);
                return dtos;
            }
        }
    }
}
